import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

import Login from '../components/Login/Login.vue'
import Manager from '../components/Manager/Manager.vue'

import Page1 from '../views/Page1/index.vue'
import Page2 from '../views/Page2/index.vue'
import Page3 from '../views/Page3/index.vue'
import Page4 from '../views/Page4/index.vue'


const router =  new Router({
  routes: [
    {
      path: '/',
      name: Login,
      component: Login
    },
    {
      path: '/login',
      name: Login,
      component: Login
    },
    {
      path: '/manager',
      name: Manager,
      component: Manager
    },
    {
      path: '/page1',
      name: Page1,
      component: Page1
    },
    {
      path: '/page2',
      name: Page2,
      component: Page2
    },
    {
      path: '/page3',
      name: Page3,
      component:Page3
    },
  ]
})

//添加路由守卫判断登录权限是否携带token值
router.beforeEach((to, from, next) => {
  if(to.path == '/login'){
    return next()
  }
  const tokenStr = window.sessionStorage.getItem('user_info')
  if(!tokenStr) {
    window.alert("验证失败，请先登录！")
    return next('/login')
  } 
  next()
})

export default router
